const dayjs = require("dayjs");
// 生成分数处理模块
exports.score = async (req, res) => {
  // 分 team_id  user_id 把分放进去
  const { id, team_id } = req.user;
  const { score } = req.body;
  const create_time = dayjs().format("YYYY-MM-DD HH:mm:ss");
  // 查询库里是否存在用户信息
  const selectUserSql = `select user_name from fa_users WHERE team_id=? AND id=?`;
  const sqlResult = await global.query(selectUserSql, [team_id, id]);
  if (sqlResult.length > 0) {
    const selectSql =
      "SELECT COUNT(*) FROM  fa_rank WHERE user_id =? and team_id=?";
    const result = await global.query(selectSql, [id, team_id]);
    if (result[0]["COUNT(*)"] > 0) {
      return res.cc("已存在用户信息,请勿重复插入");
    } else {
      const user_name = sqlResult[0]["user_name"];
      const sql = `INSERT INTO fa_rank(user_id,team_id,user_name,score,create_time) VALUES(?,?,?,?,?) `;
      global
        .query(sql, [id, team_id, user_name, score, create_time])
        .then((sqlRes) => {
          if (sqlRes.affectedRows !== 1) {
            return res.cc("插入失败");
          }
          res.cc("插入" + sqlRes.affectedRows + "条成功");
        });
    }
  } else {
    return res.cc("暂无用户信息");
  }
};
// 获取score的列表处理模块
exports.getScore = async (req, res) => {
  const sql = `SELECT * FROM fa_rank`;
  global.query(sql).then((sqlResults) => {
    console.log("sqlResults---", sqlResults);
    if (sqlResults.length > 0) {
      res.send({ code: 200, msg: "success", data: sqlResults });
    } else {
      return res.cc("暂无分数信息");
    }
  });
};
